package basic;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

public class TwinsLockTest {

    final static Lock twinsLock = new TwinsLock();

    static class Worker extends Thread{

        @Override
        public void run(){
            while(true){
                twinsLock.lock();
                System.out.println(Thread.currentThread().getName() + "获得锁");
                try{
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println(Thread.currentThread().getName());
                    TimeUnit.SECONDS.sleep(1);
                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    System.out.println(Thread.currentThread().getName() + "释放锁");
                    twinsLock.unlock();
                }
            }
        }
    }
    public static void main(String[] args) throws InterruptedException{
        for (int i = 0; i < 10; i++) {
            Worker w = new Worker();
            w.setDaemon(true);
            w.start();
        }
        for (int i = 0; i < 10; i++) {
            TimeUnit.SECONDS.sleep(1);
            System.out.println("-------");
        }
    }


}
